package cn.dnui_yxk801.dnuifood_16110100801.controller;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.Adapter.CommentsAdapter;
import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.CollectBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.CommentsBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.FoodBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.FoodModel;
import cn.dnui_yxk801.dnuifood_16110100801.model.ShopModel;
import cn.dnui_yxk801.dnuifood_16110100801.model.UserModel;

import static cn.dnui_yxk801.dnuifood_16110100801.MainActivity.userid;

//OnRetrofitListener<List<CommentsBean>>
public class FoodInfoActivity extends BaseActivity implements OnRetrofitListener,View.OnClickListener{
    private String food_id;
    private String shop_id;
    private String shop_name;
    private ImageView iv_collect;
    //private View view;//定义view用来设置fragment的layout
    private RecyclerView recyclerView;//定义RecyclerView
    private TextView foodname;
    private Button btn_purchase;
    private Button btn_collect;
    private TextView price;
    private ImageView pic;
    private static String Baseurl="http://172.24.10.175:8080/foodService/";
    //定义以goodsentity实体类为对象的数据集合
    //private ArrayList<FoodBean> foodsList = new ArrayList<>();
    //自定义recyclerveiw的适配器
    private CommentsAdapter commentsAdapter;
    @Override
    void initViews() {
        layout_file=R.layout.activity_food_info;
        setLayout(layout_file);
        foodname=findViewById(R.id.info_foodname);
        price=findViewById(R.id.info_price);
        pic=findViewById(R.id.info_pic);
        btn_purchase=findViewById(R.id.btn_purchase);
        btn_collect=findViewById(R.id.btn_collect);
        iv_collect=findViewById(R.id.iv_collect);
        recyclerView = findViewById(R.id.rv_mycomments);
        //创建默认的线性
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        // 设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true); //创建并设置Adapter
        //list = getData();
        initData();
    }

    private void initData() {
        Intent intent=getIntent();
        food_id=intent.getStringExtra("food_id");
        shop_id=intent.getStringExtra("shop_id");
        shop_name=intent.getStringExtra("shop_name");
        //shopid="1";
        FoodModel foodModel = new FoodModel();
        foodModel.getAllCommentsByFood(food_id,this);
        foodModel.getFoodById(food_id,this);
        foodModel.isCollected(userid,food_id,"1",this);
//        CollectModel collectModel=new CollectModel();
////        collectModel.isCollected(userid,food_id,"1",this);
    }

    @Override
    void initEvents() {
        btn_purchase.setOnClickListener(this);
        btn_collect.setOnClickListener(this);
    }
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_purchase:Intent intent=new Intent(FoodInfoActivity.this,FoodPurchaseActivity.class);
            intent.putExtra("foodid",food_id);
            intent.putExtra("shopid",shop_id);
            intent.putExtra("shopname",shop_name);
            startActivity(intent);
            break;
            case R.id.btn_collect:
                UserModel userModel=new UserModel();
                userModel.userCollectFood(userid,food_id,this);

        }

    }

    @Override
    void initDatas() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }




    @Override
    public void onSuccess(Object object, int flag) {
        if(flag==1){
            FoodBean foodinfo=(FoodBean)object;
            foodname.setText(foodinfo.getFoodname());
            price.setText(foodinfo.getPrice());
            Picasso.with(this).load(Baseurl+foodinfo.getPic()).into(pic);
        }
        if (flag==2){
            try {
                    ResultBean resultBean = (ResultBean) object;

                    Toast.makeText(FoodInfoActivity.this, "收藏店铺成功", Toast.LENGTH_SHORT).show();
            }
            catch (Exception e){
                List<CommentsBean> list = (List<CommentsBean>) object;
                commentsAdapter = new CommentsAdapter(this, list);
                recyclerView.setAdapter(commentsAdapter);
            }
        }
        if (flag==3){
            CollectBean collectBean=(CollectBean)object;
            if ("1".equals(collectBean.getCollected().toString())){
            iv_collect.setImageResource(android.R.drawable.btn_star_big_on);
            btn_collect.setText("取消收藏");
        }
        else{
                iv_collect.setImageResource(android.R.drawable.btn_star_big_off);
                btn_collect.setText("收藏");
        }
        }
        if (flag==7){
            CollectBean collectBean=(CollectBean)object;
            if ("0".equals(collectBean.getCollected().toString())){
                UserModel userModel=new UserModel();
                userModel.userCollectShop(userid,shop_id,this);//收藏菜谱同时收藏店铺
            }
        }
        if (flag==4){
            ResultBean resultBean=(ResultBean)object;
            if (btn_collect.getText().equals("收藏")){
                ShopModel shopModel=new ShopModel();
                shopModel.isCollected(userid,shop_id,"0",this);
                iv_collect.setImageResource(android.R.drawable.btn_star_big_on);
                btn_collect.setText("取消收藏");
                Toast.makeText(FoodInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
            }
            else {
                iv_collect.setImageResource(android.R.drawable.btn_star_big_off);
                btn_collect.setText("收藏");
                Toast.makeText(FoodInfoActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
            }
        }
    }





                                                                        @Override
    public void onFalure(String msg) {

    }
}
